﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Xml;

namespace CPPEI.WebApiTool
{
    public class WebInitiatorBuilder
    {
        StringBuilder _stringBuilder = new StringBuilder();

        List<DInitiator> _initiatorList;

        public WebInitiatorBuilder(List<DInitiator> initiatorList)
        {
            _initiatorList = initiatorList;
        }

        public void Build(string filePath)
        {
            if(File.Exists(filePath))
            {
                #region file exist
                XmlDocument xDoc = new XmlDocument();
                xDoc.Load(filePath);
                XmlNode xWebApi = xDoc.SelectSingleNode("./WebApi");
                if(xWebApi == null)
                {
                    xDoc.RemoveAll();

                    xWebApi = xDoc.CreateElement("WebApi");
                    xDoc.AppendChild(xWebApi);
                }

                XmlNode xInitiators = xDoc.SelectSingleNode("./WebApi/Initiators");
                if(xInitiators == null)
                {
                    xInitiators = xDoc.CreateElement("Initiators");
                    foreach (var type in _initiatorList)
                    {
                        XmlElement xInitiator = xDoc.CreateElement("Initiator");

                        XmlAttribute xattrAssembly = xDoc.CreateAttribute("Assembly");
                        xattrAssembly.Value = type.Assembly.FullName;
                        xInitiator.Attributes.Append(xattrAssembly);

                        XmlAttribute xattrType = xDoc.CreateAttribute("Type");
                        xattrType.Value = type.Type.FullName;
                        xInitiator.Attributes.Append(xattrType);

                        xInitiators.AppendChild(xInitiator);
                        Console.WriteLine(string.Format("write initiator:{0}", type.Type.FullName));
                    }

                    xWebApi.AppendChild(xInitiators);
                }
                else
                {
                    xInitiators.RemoveAll();
                    foreach (var type in _initiatorList)
                    {
                        XmlElement xInitiator = xDoc.CreateElement("Initiator");

                        XmlAttribute xattrAssembly = xDoc.CreateAttribute("Assembly");
                        xattrAssembly.Value = type.Assembly.FullName;
                        xInitiator.Attributes.Append(xattrAssembly);

                        XmlAttribute xattrType = xDoc.CreateAttribute("Type");
                        xattrType.Value = type.Type.FullName;
                        xInitiator.Attributes.Append(xattrType);

                        xInitiators.AppendChild(xInitiator);
                        Console.WriteLine(string.Format("write initiator:{0}", type.Type.FullName));
                    }
                }

                xDoc.Save(filePath);
                #endregion
            }
            else
            {
                XmlDocument xDoc = new XmlDocument();
                XmlNode xWebApi = xDoc.SelectSingleNode("./WebApi");
                if (xWebApi == null)
                {
                    xDoc.RemoveAll();
                    xWebApi = xDoc.CreateElement("WebApi");
                    xDoc.AppendChild(xWebApi);
                }

                XmlElement xInitiators = xDoc.CreateElement("Initiators");
                foreach (var type in _initiatorList)
                {
                    XmlElement xInitiator = xDoc.CreateElement("Initiator");

                    XmlAttribute xattrAssembly = xDoc.CreateAttribute("Assembly");
                    xattrAssembly.Value = type.Assembly.FullName;
                    xInitiator.Attributes.Append(xattrAssembly);

                    XmlAttribute xattrType = xDoc.CreateAttribute("Type");
                    xattrType.Value = type.Type.FullName;
                    xInitiator.Attributes.Append(xattrType);

                    xInitiators.AppendChild(xInitiator);
                    Console.WriteLine(string.Format("write initiator:{0}", type.Type.FullName));
                }

                xWebApi.AppendChild(xInitiators);

                xDoc.Save(filePath);
            }
        }
    }
}
